#include "../src/thread/thread.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
 * 可以使用windows下的API函数进行仿真调试终端的编码设置
 * #include <windows.h>
 * SetConsoleOutputCP(65001);
 **/

// ABDH#K###E##CFI###G#J##
// ABD#G##E##CF###
int main(int argc, const char *argv[])
{
    int ret = 0;
    BiTNode *tree = NULL, *ptttttttttt = NULL;

    // 创建一个二叉树
    printf("请按照先序输入二叉树(空节点用#表示)：");
    tree = fBiTree.create(ptttttttttt);
    ptttttttttt = NULL;

    if (tree != NULL)
    {
        printf("\n二叉树创建成功！\n");
    }
    else
    {
        printf("\n二叉树创建出现异常！\n");
        ret = -1;
        goto ERROR_END;
    }

    printf("\n先序遍历输出：");
    fBiTree.prevOrder(tree);
    printf("\n中序遍历输出：");
    fBiTree.inOrder(tree);
    printf("\n后序遍历输出：");
    fBiTree.postOrder(tree);
    printf("\n层序遍历输出：");
    fBiTree.levelOrder(tree);

    int leaf = 0;
    fBiTree.leafNum(tree, &leaf);
    printf("\n\n节点的个数 = %d  叶子节点的个数 = %d, 树的深度/高度 = %d\n",
           fBiTree.nodeNum(tree), leaf, fBiTree.depth(tree));

    BiTNode *copyTree = fBiTree.copy(tree, ptttttttttt);

    printf("\ncopy先序遍历输出：");
    fBiTree.prevOrder(copyTree);

    BiTNode *tmp = NULL;
    fthread.prevThread(copyTree, &tmp);

    printf("\ncopy树先序线索化正序遍历输出：");
    fthread.Older(copyTree);

    printf("copy树先序线索化正序遍历输出：");
    fthread.byNextOlder(copyTree);

    printf("copy树先序线索化倒序遍历输出：");
    fthread.byPrevOlder(copyTree);

    BiTNode *papra1 = copyTree;
    // B
    BiTNode *ppppppp = fthread.parent(papra1, copyTree->lchild);
    if (ppppppp != NULL)
        printf("\n%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->lchild->data, ppppppp->data, copyTree->lchild->parent->data);
    // D
    ppppppp = fthread.parent(papra1, copyTree->lchild->lchild);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->lchild->lchild->data, ppppppp->data, copyTree->lchild->lchild->parent->data);
    // E
    ppppppp = fthread.parent(papra1, copyTree->lchild->rchild);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->lchild->rchild->data, ppppppp->data, copyTree->lchild->rchild->parent->data);
    // G
    ppppppp = fthread.parent(papra1, copyTree->lchild->lchild->rchild);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->lchild->lchild->rchild->data, ppppppp->data, copyTree->lchild->lchild->rchild->parent->data);
    // C
    ppppppp = fthread.parent(papra1, copyTree->rchild);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->rchild->data, ppppppp->data, copyTree->rchild->parent->data);
    // F
    ppppppp = fthread.parent(papra1, copyTree->rchild->lchild);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->rchild->lchild->data, ppppppp->data, copyTree->rchild->lchild->parent->data);

    ppppppp = fthread.parent(papra1, copyTree);
    if (ppppppp != NULL)
        printf("%c 的双亲结点是 %c, parent指针域为 %c\n", copyTree->data, ppppppp->data, copyTree->parent->data);
    else
        printf("%c 的双亲结点是 NULL, parent指针域为 NULL\n", copyTree->data);

    // B
    printf("\n%c 是 %c 的前驱节点\n", fthread.prevNode(copyTree->lchild)->data, copyTree->lchild->data);
    // C
    printf("%c 是 %c 的前驱节点\n", fthread.prevNode(copyTree->rchild)->data, copyTree->rchild->data);
    // D
    printf("%c 是 %c 的前驱节点\n", fthread.prevNode(copyTree->lchild->lchild)->data, copyTree->lchild->lchild->data);
    // G
    printf("%c 是 %c 的前驱节点\n", fthread.prevNode(copyTree->lchild->lchild->rchild)->data, copyTree->lchild->lchild->rchild->data);
    // E
    printf("%c 是 %c 的前驱节点\n", fthread.prevNode(copyTree->lchild->rchild)->data, copyTree->lchild->rchild->data);
    // F
    printf("%c 是 %c 的前驱节点\n\n", fthread.prevNode(copyTree->rchild->lchild)->data, copyTree->rchild->lchild->data);

    tmp = NULL;

ERROR_END:
    // 释放二叉树
    fBiTree.release(tree);
    /* copyTree已经在线索化的过程中别修改，此时按照原先的释放方式释放的话出现异常 */
    // fBiTree.release(copyTree);

    printf("system exited with return code %d\n", ret);

    return ret;
}
/*
ABDH#K###E##CFI###G#J##
ABD#G##E##CF###
*/
